import akka.actor.*;

/**
 * 
 * @author Pietro Bua & Rohmin Mirza
 *
 */
public class Developer extends UntypedActor
{
	private ActorRef secretary;

	/**
	 * 
	 */
	public Developer(ActorRef sec)
	{
		secretary = sec;
	}

	/**
	 * 
	 */
	@Override
	public void preStart() throws InterruptedException
	{
		justDevelop();
		System.out.println( ">" + getSelf().path().name() + " started and getting ready to meet");
		secretary.tell(Message.REQUEST_MEETING_DEV, getSelf());
		System.out.println(getSelf().path().name() +" ready to meet and waiting for invite");
	}	
	
	/**
	 * 
	 */
	@Override
	public void onReceive(Object message) throws Exception
	{	
		if( message instanceof Message )
		{
			Message m = (Message) message;
			switch(m)
			{
				case INVITE_TRAVEL:
					justDevelop();
					System.out.println("developer ready to meet");
					getSender().tell(Message.DEV_ARRIVE, getSelf());
					break;
				case INVITE_MEETING:
					System.out.println(getSelf().path().name() + "Entering meeting");
					getSender().tell(Message.MEETING_ACCEPT, getSelf());
					break;
				case MEETING_END:
					justDevelop();
					break;
				default:unhandled(m);
			}
		}
		else
		{
			System.out.println("ERROR: Developer->onReceive - message != instanceof Message");
		}
	}
	
	/**
	 * 
	 */
	public void postStop()
	{
		System.out.println( "<" + getSelf().path().name() + " exiting" );
	}
	
	/**
	 * Makes a random time in which the developer is programming
	 */
	private void justDevelop() throws InterruptedException
	{
        int time = (int)(Math.random() * 1000);
		System.out.println(getSelf().path().name() + " living " + time + "ms");
		Thread.sleep( time );
	}
	
	/**
	 * 
	 * @param secretary
	 * @return
	 */
	public static Props prop(ActorRef secretary){
		return Props.create(Developer.class, secretary);
	}


}
